package com.sportizz.sport;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Utils {

	public static Context act_context =  StartActivity.Context_start;
	public static Activity act_c =  (Activity) StartActivity.Context_start;
	
	public static String jsonString = null;

	public static String getJSONString(String url) {

		if (isNetworkAvailable(act_c) == true) {

			HttpURLConnection linkConnection = null;
			try {
				URL linkurl = new URL(url);
				linkConnection = (HttpURLConnection) linkurl.openConnection();
				int responseCode = linkConnection.getResponseCode();
				if (responseCode == HttpURLConnection.HTTP_OK) {
					InputStream linkinStream = linkConnection.getInputStream();
					ByteArrayOutputStream baos = new ByteArrayOutputStream();
					int j = 0;
					while ((j = linkinStream.read()) != -1) {
						baos.write(j);
					}
					byte[] data = baos.toByteArray();
					jsonString = new String(data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return "null";
			} finally {
				if (linkConnection != null) {
					linkConnection.disconnect();
				}
			}

			return jsonString;
		} else {
			AlertDialog.Builder build = new AlertDialog.Builder(act_context);
			build.setCancelable(false);
			build.setTitle("Check Internet Connection..");
			build.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});

			AlertDialog alert = build.create();
			alert.show();
			return "null";
		}
	}

	public static String readJsonFeed(String rss_url, Context act) {
		if (isNetworkAvailable(act_c) == true) {
			StringBuilder builder = new StringBuilder();
			HttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(rss_url);
			try {
				HttpResponse response = client.execute(httpGet);
				StatusLine statusLine = response.getStatusLine();
				int statusCode = statusLine.getStatusCode();
				if (statusCode == 200) {
					HttpEntity entity = response.getEntity();
					InputStream content = entity.getContent();
					BufferedReader reader = new BufferedReader(
							new InputStreamReader(content));
					String line;
					while ((line = reader.readLine()) != null) {
						builder.append(line);
					}
				} else {
					Log.e("suraj", "Failed to download file");
					return "null";
				}
			} catch (Exception e) {

				Thread t = new Thread(new Runnable() {

					@Override
					public void run() {

						act_c.runOnUiThread(new Runnable() {

							@Override
							public void run() {

								AlertDialog.Builder build = new AlertDialog.Builder(
										act_context);
								build.setCancelable(false);
								build.setTitle("Check Internet Connection..");
								build.setPositiveButton("Yes",
										new DialogInterface.OnClickListener() {
											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();
											}
										});

								AlertDialog alert = build.create();
								alert.show();

							}
						});
					}
				});

				t.start();

				return "null";
			}
			return builder.toString();
		} else {
			Thread t = new Thread(new Runnable() {

				@Override
				public void run() {

					act_c.runOnUiThread(new Runnable() {

						@Override
						public void run() {

							AlertDialog.Builder build = new AlertDialog.Builder(
									act_context);
							build.setCancelable(false);
							build.setTitle("Check Internet Connection..");
							build.setPositiveButton("Yes",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									});

							AlertDialog alert = build.create();
							alert.show();

						}
					});
				}
			});

			t.start();

			return "null";
		}
	}

	public static boolean isNetworkAvailable(Activity activity) {
		try{
		ConnectivityManager connectivity = (ConnectivityManager) activity
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		}catch (Exception e) {
		}
		return false;
	}

}
